<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller{
    
    function  __construct(){
        parent::__construct();
    }
}
/**
 * 
 * @author liubin-bt
 * @property ProjectFollows_model $ProjectFollows_model
 */
class Base extends CI_Controller{

    protected $uid;
    
    protected $user;
    
    public function __construct(){
        parent::__construct();
        if( isset($_REQUEST['debug']) == 1 ){
            $this->uid = 1;
            $user['uid'] = 1;
            $user['group_id'] = 1;
            $user['corp_id'] = 1;
            $this->user = $user;
        }
        $this->login();
        if( !$this->uid ){
            $this->outJsonData(-1, '用户未登录！');
        }
    }
    
    public function login(){
        if( !empty($this->session->user) ){
            $this->user = $this->session->user;
            $this->uid = $this->user['uid'];
        }
    }
    
    /**
     * 单文件上传
     * @param string $field
     */
    protected function single_upload($field){
        $this->load->library('upload');
        $data = [];
        $this->upload->do_upload($field);
        if( !$this->upload->do_upload($field) ){
            $this->outJsonData(-1, $this->upload->display_errors());
        } else {
            $data = $this->upload->data();
        }
        return $data ? $data : [];
    }
    
    /**
     * 权限校验
     * 判断是否是管理员或者项目成员
     * @param int $project_id
     * @return boolean
     */
    protected function auth_invaid($project_id){
        if( !$project_id ){
            $this->outJsonData(-1, '项目id不能为空');
        }
        //管理员
        if( $this->user['group_id'] == 1 ){
            return true;
        }
        //成员
        $this->load->model('ProjectFollows_model');
        $uids = $this->ProjectFollows_model->getUidsByProjectId($project_id);
        if( in_array($this->user['uid'], $uids) ){
            return true;
        }
        return false;
    }

    protected function outJsonData($errno, $msg){
        $data['errorno'] = $errno;
        $data['data'] = $msg;
        echo json_encode($data);
        exit;
    }

    protected function getSafeRequest($key, $default=''){
    	if( !is_array($_POST) ){
    		$_POST = [];
    	}
    	if( !is_array($_GET) ){
    		$_GET = [];
    	}
        $_REQUEST = array_merge($_GET, $_POST);
        if( isset($_REQUEST[$key]) ){
            $str = $_REQUEST[$key];
        } else {
            return $default;
        }
        $str = trim($str);
        $str = htmlspecialchars($str, ENT_QUOTES);
        if (!@get_magic_quotes_gpc()) { // 判断magic_quotes_gpc是否打开
            $str = addslashes($str);
        }
        return $str;
    }
    
    protected function getSafePost($key, $default=''){
        if( isset($_POST[$key]) ){
            $str = $_POST[$key];
        } else {
            return $default;
        }
        $str = trim($str);
        $str = htmlspecialchars($str, ENT_QUOTES);
        if (!@get_magic_quotes_gpc()) { // 判断magic_quotes_gpc是否打开
            $str = addslashes($str);
        }
        return $str;
    }
    
    protected function getCityInfos() {
        $jsonstr = file_get_contents(BASEPATH.'core/citysjson.txt');
        $citys = json_decode($jsonstr, true);
        $temp = [];
        foreach ($citys as $p){
            $temp['0_'.$p['value']] = $p['label'];
            if( !empty($p['children']) ){
                foreach( $p['children'] as $child){
                    $temp['1_'.$child['value']] = $child['label'];
                    if( !empty($child['children']) ){
                        foreach ($child['children'] as $threech){
                            $temp['2_'.$threech['value']] = $threech['label'];
                        }
                    }
                }
            }
        }
        return $temp;
    }
    
    public function getCityByIds( $list, $ids ){
        $return = [];
        foreach ($ids as $k=>$id){
            if( isset($list[$k.'_'.$id]) ){
                $return[] = $list[$k.'_'.$id];
            }
        }
        return $return;
    }
}
?>